#include "sdl2_tp.h"
#include <stdio.h>

#define debug

int sdl2_tp_init(sdl2_tp* tp)
{
	if (NULL == tp) {
		return -1;
	}

	/* do nothing */

	return 0;
}

void sdl2_tp_deinit(sdl2_tp* tp)
{
	if (NULL == tp) {
		return;
	}
}

int sdl2_tp_event_poll(sdl2_tp* tp)
{
	SDL_Event event;

	while (SDL_PollEvent(&event)) {
		if (SDL_QUIT == event.type) {
			tp->priv.quit = true;
			debug("quit\n");
		} else if (SDL_MOUSEBUTTONDOWN == event.type) {
			switch (event.button.button) {
			case SDL_BUTTON_LEFT:
				tp->priv.tp.pressed = true;
				tp->priv.tp.x = event.motion.x;
				tp->priv.tp.y = event.motion.y;
				debug("mouse left down\n");
				break;
			case SDL_BUTTON_RIGHT:
				debug("mouse right down\n");
				break;
			case SDL_BUTTON_MIDDLE:
				tp->priv.wl.pressed = true;
				debug("mouse middle down\n");
				break;
			default:
				break;
			}

		} else if (SDL_MOUSEBUTTONUP == event.type) {
			switch (event.button.button) {
			case SDL_BUTTON_LEFT:
				tp->priv.tp.pressed = false;
				tp->priv.tp.x = event.motion.x;
				tp->priv.tp.y = event.motion.y;
				debug("mouse left up\n");
				break;
			case SDL_BUTTON_RIGHT:
				debug("mouse right up\n");
				break;
			case SDL_BUTTON_MIDDLE:
				tp->priv.wl.pressed = false;
				debug("mouse middle up\n");
				break;
			default:
				break;
			}
		} else if (SDL_MOUSEMOTION == event.type) {
			tp->priv.tp.x = event.motion.x;
			tp->priv.tp.y = event.motion.y;
			debug("mouse motion (%d, %d)\n", event.motion.x, event.motion.y);
		} else if (SDL_KEYDOWN == event.type) {
			switch (event.key.keysym.sym) {
			case SDLK_UP:
				tp->priv.kp.prev_pressed = true;
				debug("key up\n");
				break;
			case SDLK_DOWN:
				tp->priv.kp.next_pressed = true;
				debug("key down\n");
				break;
			case SDLK_LEFT:
				tp->priv.kp.left_pressed = true;
				debug("key left\n");
				break;
			case SDLK_RIGHT:
				tp->priv.kp.right_pressed = true;
				debug("key right\n");
				break;
			case SDLK_RETURN:
				tp->priv.kp.enter_pressed = true;
				debug("key enter\n");
				break;
			default:
				break;
			}
		} else if (SDL_KEYUP == event.type) {
			switch (event.key.keysym.sym) {
			case SDLK_UP:
				tp->priv.kp.prev_pressed = false;
				debug("key up release\n");
				break;
			case SDLK_DOWN:
				tp->priv.kp.next_pressed = false;
				debug("key down release\n");
				break;
			case SDLK_LEFT:
				tp->priv.kp.left_pressed = false;
				debug("key left release\n");
				break;
			case SDLK_RIGHT:
				tp->priv.kp.right_pressed = false;
				debug("key right release\n");
				break;
			case SDLK_RETURN:
				tp->priv.kp.enter_pressed = false;
				debug("key enter release\n");
				break;
			default:
				break;
			}
		} else if (SDL_MOUSEWHEEL == event.type) {
			tp->priv.wl.offset = event.wheel.y;
			debug("wheel roll (%d, %d)\n", event.wheel.x, event.wheel.y);
		}
	}

	return 0;
}

bool sdl2_tp_quit_get(sdl2_tp* tp)
{
	if (NULL == tp) {
		return false;
	}

	return tp->priv.quit;
}

int sdl2_tp_get_xy(sdl2_tp* tp, int* x, int* y)
{
	if (NULL == tp) {
		return -1;
	}

	if (NULL == x) {
		return -1;
	}

	if (NULL == y) {
		return -1;
	}

	*x = tp->priv.tp.x;
	*y = tp->priv.tp.y;

	return 0;
}

bool sdl2_tp_is_pressed(sdl2_tp* tp)
{
	if (NULL == tp) {
		return false;
	}

	return tp->priv.tp.pressed;
}

int sdl2_key_get_keycode(sdl2_tp* tp)
{
	if (NULL == tp) {
		return false;
	}

	if (tp->priv.kp.prev_pressed) {
		return SDL2_KEY_PREV;
	} else if (tp->priv.kp.next_pressed) {
		return SDL2_KEY_NXET;
	} else if (tp->priv.kp.left_pressed) {
		return SDL2_KEY_LEFT;
	} else if (tp->priv.kp.right_pressed) {
		return SDL2_KEY_RIGHT;
	} else if (tp->priv.kp.enter_pressed) {
		return SDL2_KEY_ENTER;
	}

	return 0;
}

bool sdl2_wheel_get_pressed(sdl2_tp* tp)
{
	if (NULL == tp) {
		return false;
	}

	return tp->priv.wl.pressed;
}

int sdl2_wheel_get_offset(sdl2_tp* tp)
{
	int offset = 0;

	if (NULL == tp) {
		return -1;
	}

	offset = tp->priv.wl.offset;

	/* 每次读取后，手动清零 */
	tp->priv.wl.offset = 0;

	return offset;
}
